import 'package:event_bus/event_bus.dart';

class EventData<T> {
  int type; //发送事件的类型
  int? childType; //发送事件的二级类型
  T? data; //发送事件的数据

  EventData(this.type, {this.childType, this.data});

  factory EventData.build(int type, {int? childType, T? data}) =>
      EventData(type, childType: childType, data: data);
}

class MyEventBus {
  // 工厂模式
  factory MyEventBus() => _getInstance();

  static MyEventBus get instance => _getInstance();
  static  MyEventBus? _instance;
  late EventBus eventBus;

  MyEventBus._internal() {
    // 初始化
    init();
  }

  static MyEventBus _getInstance() {
    if (_instance == null) {
      _instance = new MyEventBus._internal();
    }
    return _instance!;
  }

  Stream<T> on<T>() {
    return eventBus.on<T>();
  }

  /// Fires a new event on the event bus with the specified [event].
  ///
  void fire(event) {
    eventBus.fire(event);
  }

  void fireEventData<T>(int type, {int? childType, T? data}) {
    fire(EventData<T>.build(type, childType: childType, data: data));
  }

  void init() {
    eventBus = new EventBus();
  }
}
